{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "'''\n",
    "【课程5.2】  条件判断：if语句\n",
    "\n",
    "Python条件语句是通过一条或多条语句的执行结果（True或者False）来决定执行的代码块。\n",
    "\n",
    "if 判断条件：\n",
    "    执行语句……\n",
    "else：\n",
    "    执行语句……\n",
    "    \n",
    "** 开始有缩进的概念\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18岁以下不宜观看\n"
     ]
    }
   ],
   "source": [
    "# 基本判断语句\n",
    "\n",
    "age = 12\n",
    "if age < 18:\n",
    "    print('18岁以下不宜观看')\n",
    "# if语句后面必须有 ： \n",
    "# 自动缩进\n",
    "# if语句写完后，要退回原有缩进继续写\n",
    "# Python代码的缩进规则：具有相同缩进的代码被视为代码块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入成绩：60\n",
      "该学生成绩为：60\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "# 输入函数 input()\n",
    "\n",
    "score = input('请输入成绩：')\n",
    "print('该学生成绩为：' + score)\n",
    "print(type(score))\n",
    "# 注意：input()返回结果都为字符串，如果需要变为数字则用到int()/float()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "luren\n"
     ]
    }
   ],
   "source": [
    "# 两种条件判断:if-else\n",
    "\n",
    "flag = False\n",
    "name = 'luren'\n",
    "if name == 'python':          # 判断变量否为'python'\n",
    "    flag = True               # 条件成立时设置标志为真\n",
    "    print( 'welcome boss')    # 并输出欢迎信息\n",
    "else:\n",
    "    print(name)               # 条件不成立时输出变量名称"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "user\n"
     ]
    }
   ],
   "source": [
    "# 多种条件判断:if-elif-...-else\n",
    "\n",
    "num = 2    \n",
    "if num == 3:            # 判断num的值\n",
    "    print('boss')       \n",
    "elif num == 2:\n",
    "    print('user')\n",
    "elif num == 1:\n",
    "    print('worker')\n",
    "elif num < 0:           # 值小于零时输出\n",
    "    print('error')\n",
    "else:\n",
    "    print('roadman')    # 条件均不成立时输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n",
      "undefine\n",
      "undefine\n"
     ]
    }
   ],
   "source": [
    "# 单语句多条件判断：or and\n",
    "\n",
    "num = 5\n",
    "if num >= 0 and num <= 10:    \n",
    "    print( 'hello')\n",
    "# 判断值是否在0~10之间\n",
    "# 输出结果: hello\n",
    " \n",
    "num = 10\n",
    "if num < 0 or num > 10:    \n",
    "    print( 'hello')\n",
    "else:\n",
    "    print( 'undefine')\n",
    "# 判断值是否在小于0或大于10\n",
    "# 输出结果: undefine\n",
    " \n",
    "num = 8\n",
    "if (num >= 0 and num <= 5) or (num >= 10 and num <= 15):    \n",
    "    print( 'hello')\n",
    "else:\n",
    "    print( 'undefine')\n",
    "# 判断值是否在0~5或者10~15之间\n",
    "# 输出结果: undefine"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "小作业\n",
    "\n",
    "① 编写一个简单的判断语句代码：输入某个成绩，如果成绩分数大于或等于60分，则返回及格，小于60分，则返回不及格\n",
    "\n",
    "② 编写猜数字小游戏的代码：输入一个数字，分别针对猜对数字、猜错数字、输入错误给予判断"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "'''\n",
    "【课程5.3】  循环语句：for循环\n",
    "\n",
    "for循环可以遍历任何序列的项目，如一个列表或者一个字符串。\n",
    "\n",
    "迭代\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world!\n",
      "hello world!\n",
      "hello world!\n",
      "hello world!\n",
      "hello world!\n"
     ]
    }
   ],
   "source": [
    "# 想输出\"hello world\"5次怎么办？\n",
    "for i in range(5):\n",
    "    print('hello world!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "2\n",
      "4\n",
      "6\n",
      "8\n",
      "-----\n",
      "Mary年龄为：20岁\n",
      "Tom年龄为：18岁\n",
      "Alex年龄为：17岁\n",
      "Jack年龄为：19岁\n"
     ]
    }
   ],
   "source": [
    "# 通过for遍历序列、映射\n",
    "\n",
    "lst = list(range(10))\n",
    "for i in lst[::2]:\n",
    "    print(i)\n",
    "print('-----')\n",
    "# 遍历list\n",
    "\n",
    "age = {'Tom':18, 'Jack':19, 'Alex':17, 'Mary':20}\n",
    "for name in age:\n",
    "    print(name + '年龄为：%s岁' % age[name])\n",
    "# 遍历字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 0\n",
      "0 1\n",
      "1 0\n",
      "1 1\n",
      "2 0\n",
      "2 1\n"
     ]
    }
   ],
   "source": [
    "# 嵌套循环\n",
    "\n",
    "for i in range(3):\n",
    "    for j in range(2):\n",
    "        print(i,j)\n",
    "# 循环套循环，注意：尽量不要多于3个嵌套"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "小作业\n",
    "\n",
    "① 生成一个数值列表，用for循环打印出所有元素\n",
    "\n",
    "② 用for循环遍历一个字符串，打印出各个字母\n",
    "\n",
    "③ 生成一个字典，分别打印出key和value\n",
    "\n",
    "④ 用input输入一个循环次数n，打印hello world n遍\n",
    "\n",
    "⑤ 码一个等差数列，四个变量：首项a，项数n，公差d，求和s，这几个参数都可通过input()输入\n",
    "\n",
    "⑥ 两组列表[\"a\", \"b\", \"c\"],[1,2,3]，用for循环把它们组成一个字典，一一对应"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "'''\n",
    "【课程5.4】  循环语句：while循环\n",
    "\n",
    "执行语句可以是单个语句或语句块\n",
    "\n",
    "判断条件可以是任何表达式，任何非零、或非空（null）的值均为true。\n",
    "\n",
    "当判断条件假false时，循环结束。\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The count is: 0\n",
      "The count is: 1\n",
      "The count is: 2\n",
      "The count is: 3\n",
      "The count is: 4\n",
      "The count is: 5\n",
      "The count is: 6\n",
      "The count is: 7\n",
      "The count is: 8\n",
      "Good bye!\n"
     ]
    }
   ],
   "source": [
    "# 基本运行逻辑\n",
    "\n",
    "count = 0\n",
    "while count < 9:\n",
    "    print( 'The count is:', count)\n",
    "    count = count + 1\n",
    "print( \"Good bye!\")\n",
    "# 这里count<9是一个判断语句，当判断为True时，则继续运行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 关于无限循环：如果条件判断语句永远为 true，循环将会无限的执行下去\n",
    "\n",
    "var = 1\n",
    "while var == 1 :  \n",
    "    num = input(\"Enter a number  :\")\n",
    "    print( \"You entered: \", num)\n",
    "print( \"Good bye!\")\n",
    "# 该条件永远为true，循环将无限执行下去\n",
    "# 一定要避免无限循环！！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0  is  less than 5\n",
      "1  is  less than 5\n",
      "2  is  less than 5\n",
      "3  is  less than 5\n",
      "4  is  less than 5\n",
      "5  is not less than 5\n"
     ]
    }
   ],
   "source": [
    "# while-else语句\n",
    "\n",
    "count = 0\n",
    "while count < 5:\n",
    "    print(count, \" is  less than 5\")\n",
    "    count = count + 1\n",
    "else:\n",
    "    print(count, \" is not less than 5\")\n",
    "# 逻辑和if-else一样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "'''\n",
    "【课程5.5】  循环控制语句\n",
    "\n",
    "break：在语句块执行过程中终止循环，并且跳出整个循环\n",
    "\n",
    "continue：在语句块执行过程中跳出该次循环，执行下一次循环\n",
    "\n",
    "pass：pass是空语句，是为了保持程序结构的完整性\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "190\n",
      "第0次计算\n",
      "第1次计算\n",
      "第2次计算\n",
      "第3次计算\n",
      "第4次计算\n",
      "第1次计算\n",
      "第2次计算\n",
      "第3次计算\n",
      "第4次计算\n",
      "第5次计算\n",
      "第2次计算\n",
      "第3次计算\n",
      "第4次计算\n",
      "第5次计算\n",
      "第6次计算\n",
      "结果为30\n"
     ]
    }
   ],
   "source": [
    "# break语句\n",
    "\n",
    "s = 0\n",
    "n = 1\n",
    "while n > 0:\n",
    "    s = s + n\n",
    "    n = n + 1\n",
    "    if n == 20:\n",
    "        break\n",
    "print(s)\n",
    "# break语句用来终止循环语句，即便循环条件没有False条件或者序列还没被完全递归完，也会停止执行循环语句。\n",
    "\n",
    "s = 0\n",
    "for i in range(10):\n",
    "    for j in range(5):\n",
    "        s = s + (i*j)\n",
    "        print('第%i次计算' %(i+j))\n",
    "    if s > 20:\n",
    "        break\n",
    "print('结果为%i' % s)\n",
    "# 如果使用嵌套循环，break语句将停止执行最深层的循环，并开始执行下一行代码。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第0次计算\n",
      "第1次计算\n",
      "第2次计算\n",
      "第3次计算\n",
      "第4次计算\n",
      "第5次计算\n",
      "第6次计算\n",
      "第7次计算\n",
      "第8次计算\n",
      "第9次计算\n",
      "第10次计算\n",
      "第11次计算\n",
      "第12次计算\n",
      "第13次计算\n",
      "第14次计算\n",
      "第15次计算\n",
      "第16次计算\n",
      "第17次计算\n",
      "第18次计算\n",
      "第19次计算\n",
      "第20次计算\n",
      "第21次计算\n",
      "第22次计算\n",
      "第23次计算\n",
      "第24次计算\n",
      "结果为600\n"
     ]
    }
   ],
   "source": [
    "# continue语句\n",
    "\n",
    "s = 0\n",
    "for i in range(50):\n",
    "    if i%2 == 0:\n",
    "        s += i\n",
    "    else:\n",
    "        continue\n",
    "    print('第%i次计算'%(i/2))\n",
    "print('结果为%i' % s)\n",
    "# continue 语句用来告诉Python跳过当前循环的剩余语句，然后继续进行下一轮循环。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前字母 : P\n",
      "当前字母 : y\n",
      "当前字母 : t\n",
      "当前字母 : h，但是我pass了\n",
      "当前字母 : h\n",
      "当前字母 : o\n",
      "当前字母 : n\n",
      "Good bye!\n"
     ]
    }
   ],
   "source": [
    "# pass语句\n",
    "\n",
    "for letter in 'Python':\n",
    "    if letter == 'h':\n",
    "        pass\n",
    "        print( '当前字母 : h，但是我pass了')\n",
    "    print( '当前字母 :', letter)\n",
    "print( \"Good bye!\")\n",
    "# pass是空语句，是为了保持程序结构的完整性。（不中断也不跳过）"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
